using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using RukouMVC.Helpers;
using RukouMVC.Models;

namespace RukouMVC.Controllers
{


    public class TopicController : BaseController
    {



        public ActionResult Index(int? page)
        {
            const int pageSize = 20;

            var query = from t in rdc.Topics  orderby t.ShowDate descending select t;

            var paginatedTopics = new PaginatedList<Topic>(query, page ?? 0, pageSize);

            return View(paginatedTopics);
        }



        public ActionResult Details(int id, int? page)
        {
            Topic topic = rdc.Topics.SingleOrDefault(t => t.TopicId == id);
            ViewData["page"] = page;
            return View(topic);
        }



        public ActionResult Create()
        {
            if (cookie["UserId"] == null || cookie["UserId"] == string.Empty)
                return Redirect("/login");

            Topic topic = new Topic();
            topic.ShowDate = DateTime.Now;
            return View(topic);
        } 



        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                if (cookie["UserId"] != null && cookie["UserId"] != string.Empty)
                {
                    Topic t = new Topic();
                    t.CreatedBy = int.Parse(cookie["UserId"]);
                    t.ShowDate = DateTime.Parse((collection["ShowDate"]));
                    t.Title = collection["Title"];
                    t.Content = collection["Content"];
                    t.ReplyNum = 0;
                    t.CreatedAt = DateTime.Now;
                    t.UpdatedAt = DateTime.Now;

                    rdc.Topics.InsertOnSubmit(t);
                    rdc.SubmitChanges();
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }


        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Reply(FormCollection collection)
        {
            try
            {
                if (cookie["UserId"] != null && cookie["UserId"] != string.Empty)
                {
                    Reply r = new Reply();
                    r.Content = collection["Content"].Replace("\r\n","<br/>");
                    r.RepliedBy = int.Parse(cookie["UserId"]);
                    r.RepliedAt = DateTime.Now;
                    r.TopicId = int.Parse(collection["TopicId"]);

                    rdc.Replies.InsertOnSubmit(r);
                    rdc.SubmitChanges();
                }
                return Redirect("/topic/" + collection["TopicId"]);
            }
            catch
            {
                return View();
            }
        }


 
        public ActionResult Edit(int id)
        {
            return View();
        }



        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
